package com.andong.exception;

import com.alibaba.fastjson.JSONObject;
import com.andong.domain.pojo.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import static com.andong.utils.SystemConstant.NOT_LOGIN_MESSAGE;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)//捕获所有异常
    public Result ex(Exception ex) {
        ex.printStackTrace();
        String msg = ex.getMessage();
        if (msg.contains("Duplicate entry")) {
            System.out.println(msg);
            if (msg.contains("phone_uindex")) {
                return Result.error("手机号已经被其他账号绑定！");
            }
            if (msg.contains("username_uindex")) {
                return Result.error("用户名存在！");
            }
            if (msg.contains("id_number_uindex")) {
                return Result.error("身份证号已经存在！");
            }
        }
        if (msg.contains("\"file\" is null")) {
            return Result.error("上传的文件不能为空！");
        }
        if (msg.contains("Maximum upload size exceeded")) {
            return Result.error("上传失败！");
        }
        return Result.error("对不起,操作失败,请联系管理员");
    }
}
